package com.example.administrator.jokeyboarddemo.keyboard;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Paint;
import android.text.Editable;
import android.text.Spannable;
import android.text.SpannableString;
import android.text.Spanned;
import android.text.style.ImageSpan;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.ScrollView;
import android.widget.TextView;

import com.example.administrator.jokeyboarddemo.FractionView;
import com.example.administrator.jokeyboarddemo.R;
import com.example.administrator.jokeyboarddemo.fillblank.FillBlank;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by gongshuobo on 2016/11/21.
 */

public class LayoutEditText extends LinearLayout {


   private List<EditText> editTextList;

    private int currentClickEdit = 0;

    private int lastClickEdit = 0;

    private OnItemEditClick onItemEditClick;


    public LayoutEditText(Context context, AttributeSet attrs) {
        super(context, attrs);
        editTextList = new ArrayList<>();
    }


    /**
     * 获取当前布局中点击的EdiText
     */

    public EditText getCurrentEditText(){
        return editTextList.get(currentClickEdit);
    }

    /**
     * 更换下一个EditText
     */

    public void setNextEditText(){
        if(currentClickEdit<editTextList.size()-1){
            currentClickEdit++;
            editTextList.get(lastClickEdit).setBackgroundResource(R.drawable.edittext_border);
            editTextList.get(currentClickEdit).setBackgroundResource(R.drawable.edittext_border_pressed);
            lastClickEdit = currentClickEdit;
        }

    }

    /**
     * 更换上一个EditText
     */
    public void setLastEditText(){
        if(currentClickEdit!=0 && currentClickEdit>0){
            currentClickEdit--;
            editTextList.get(lastClickEdit).setBackgroundResource(R.drawable.edittext_border);
            editTextList.get(currentClickEdit).setBackgroundResource(R.drawable.edittext_border_pressed);
            lastClickEdit = currentClickEdit;
        }

    }

    /**
     * 生成指定数量的编辑框
     */

    private void signEditText(int count){
        for(int i=0;i<count;i++){
            EditText editText = new EditText(getContext());
            editText.setFocusable(false);
            editTextList.add(editText);
            addView(editText,getKeyBoardNavigationParams());
        }
    }

    /**
     * 删除指定数量的编辑框
     */

    private void deleteEditText(int count){
        for(int i=0;i<count;i++){
            Log.e("aaa","删除编辑框了");
            removeView(editTextList.get(editTextList.size()-1));
            editTextList.remove(editTextList.size()-1);
        }
    }


    /**
     * 为布局生成EditText
     */

    public void setLayoutEditText(List<FillBlank> fillBlankList){
        String[] strings = new String[fillBlankList.size()];
        for(int i=0;i<fillBlankList.size();i++){
            strings[i] = fillBlankList.get(i).getAnswer();
        }
        if(fillBlankList.size()==editTextList.size()){
            setEditTextListText(strings);
        }else if(fillBlankList.size()>editTextList.size()){
            signEditText(fillBlankList.size()-editTextList.size());
            setEditTextListText(strings);
        }else if(fillBlankList.size()<editTextList.size()) {
            Log.e("aaa","小于编辑框数量,删除");
            deleteEditText(editTextList.size()-fillBlankList.size());
        }
//            linearLayouts = new LinearLayout[fillBlankList.size()];
//            editTexts = new EditText[fillBlankList.size()];
//            for(int i=0;i<fillBlankList.size();i++){
//                linearLayouts[i] = new LinearLayout(getContext());
//                linearLayouts[i].setPadding(10,10,10,10);
//                linearLayouts[i].setOrientation(LinearLayout.HORIZONTAL);
//                TextView textView = new TextView(getContext());
//                textView.setText("填空"+(i+1));
//                TextView textView1 = new TextView(getContext());
//                textView1.setText("填空描述");
//                editTexts[i] = new EditText(getContext());
//                editTexts[i].setId(i);
//                editTexts[i].setHint("填空"+(i+1));
//                editTexts[i].setFocusable(false);
//                if(i==0){
//                    editTexts[i].setBackgroundResource(R.drawable.edittext_border_pressed);
//                }else{
//                    editTexts[i].setBackgroundResource(R.drawable.edittext_border);
//                }
//                final int currentindex = i;
//                editTexts[i].setOnClickListener(new OnClickListener() {
//                    @Override
//                    public void onClick(View v) {
//                        if(onItemEditClick!=null){
//                            //改变上一个编辑框的样式
//                            currentClickEdit = currentindex;
//                            editTexts[lastClickEdit].setBackgroundResource(R.drawable.edittext_border);
//                            editTexts[currentClickEdit].setBackgroundResource(R.drawable.edittext_border_pressed);
//                            lastClickEdit = currentClickEdit;
//                            onItemEditClick.onItemClick(currentClickEdit);
//                        }
//                    }
//                });
//                //使用权重设置比例
//            linearLayouts[i].addView(textView,getWeightParams(1.0f));
//            linearLayouts[i].addView(editTexts[i],getWeightParams(2.0f));
//            linearLayouts[i].addView(textView1,getWeightParams(1.0f));
//            addView(linearLayouts[i],getKeyBoardNavigationParams());
    }

    /**
     * 设置所有编辑框的文本
     */

    public void setEditTextListText(String[] texts){
        for(int i=0;i<editTextList.size();i++){
            editTextList.get(i).setText(texts[i]);
        }
    }

    /**
     * 每个EditText的布局参数
     * @return
     */
    public RelativeLayout.LayoutParams getKeyBoardNavigationParams(){
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    }

    /** ScrollView线性布局参数**/

    public RelativeLayout.LayoutParams getMatchParams(){
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.MATCH_PARENT,RelativeLayout.LayoutParams.MATCH_PARENT);
    }

    /** 控件自适应布局**/
    public RelativeLayout.LayoutParams getWrapParams(){
        return new RelativeLayout.LayoutParams(RelativeLayout.LayoutParams.WRAP_CONTENT,RelativeLayout.LayoutParams.WRAP_CONTENT);
    }

    /**
     * 权重比例布局参数
     */

    public LinearLayout.LayoutParams getWeightParams(float weight){
        return new LayoutParams(0, LayoutParams.WRAP_CONTENT,weight);
    }

    public void setOnItemClick(OnItemEditClick onItemEditClick){
        this.onItemEditClick = onItemEditClick;
    }

    public interface OnItemEditClick{
        void onItemClick(int currentClick);
    }

    public void displayBitmapOnText(Bitmap thumbnailBitmap) {
        SpannableString span = new SpannableString("1");
        if(thumbnailBitmap == null){
            return;
        }

        int start = getCurrentEditText().getSelectionStart();
        span.setSpan(new ImageSpan(thumbnailBitmap) , span.length() - 1, span.length(), Spanned.SPAN_EXCLUSIVE_EXCLUSIVE);
        if(getCurrentEditText() != null) {
            Editable et = getCurrentEditText().getText();
            et.insert(start, span);
            getCurrentEditText().setText(et);
            getCurrentEditText().setSelection(start + span.length());
        }
        getCurrentEditText().setLineSpacing(10f, 1f);
    }

    private void showFraction(Bitmap bitmap,EditText editText){
        ImageSpan imageSpan = new ImageSpan(getContext(), bitmap);
        // 创建一个SpannableString对象，以便插入用ImageSpan对象封装的图像
        String tempUrl = "加载中...";
        SpannableString spannableString = new SpannableString(tempUrl);
        // 用ImageSpan对象替换你指定的字符串
        spannableString.setSpan(imageSpan, 0, tempUrl.length(), Spannable.SPAN_EXCLUSIVE_EXCLUSIVE);
        // 将选择的图片追加到EditText中光标所在位置
        int index = editText.getSelectionStart(); // 获取光标所在位置
        Editable edit_text = editText.getEditableText();
        if (index < 0 || index >= edit_text.length()) {
            edit_text.append(spannableString);
        } else {
            edit_text.insert(index, spannableString);
        }
        System.out.println("插入的图片：" + spannableString.toString());
    }

    public void deleteEditTextSpan() {
        Spanned s = getCurrentEditText().getEditableText();
        ImageSpan[] imageSpan = s.getSpans(0, s.length(), ImageSpan.class);
        for (int i = imageSpan.length - 1; i >= 0; i--) {
            if(i == imageSpan.length - 1) {
                int start = s.getSpanStart(imageSpan[i]);
                int end = s.getSpanEnd(imageSpan[i]);
                Editable et = getCurrentEditText().getText();
                et.delete(start, end);
            }
        }
        getCurrentEditText().invalidate();
    }

}
